<?
session_start();
include('../config.php');
include("../classes/connect.php");
require_once('../classes/class.jira.php');

if(isset($_GET['fromdate']) AND isset($_GET['todate'])){
    
    $post = $_GET['post'];
    $assignee = $_GET['assignee'];

    $priority=$_GET['priority'];
     $type=$_GET['type'];
//    if($type==='Open CV')
//    {                       
//        $type='OpenCV';
//    }
//    else if($type==='Short Listed')
//    {
//        $type='Short Listed';
//       
//    }
//    else if($type==='Test Called')
//    {
//        $type='Test Called';
//     
//    }
//    else if($type==='Rejected')
//    {
//        $type='Rejected';
//       
//    }
//    else if($type==='Interviewed')
//    {
//        $type='Interviewed';
//          
//    }  
//    else if($type==='Tested')
//    {
//        $type='Tested';
//    
//    }
//    else if($type==='Selected')
//    {
//        $type='Selected';
//     
//    }
//    else if($type==='Offered')
//    {
//        $type='Offered';
//      
//    }
//    else if($type==='Interviewed Called')
//    {
//        $type='Interview Called';
//      
//    }
//    else if($type==='Final Interview Called')
//    {
//        $type='Final Interview Call';
//      
//    }
//    else if($type==='Final Interviewed')
//    {
//      $type='Final Interviewed';
//    }
//    else if($type==='Joined')
//    {
//      $type='Joine';
//    }


    if($_GET['cat']==='year')
    {
        $arr_out = array();
      if(($post==='') && ($assignee==='') &&($priority===''))
      {
          $shortlist = "select a.*, a.currentAssigneeName,a.id,a.email,a.subject,concat( FROM_UNIXTIME(a.dateReceived)) as receiv,concat( FROM_UNIXTIME(a.modifiedDate)) as modi,a.modifiedBy,a.applicantName,a.priority from trn_emaildetails a 
                where a.deleted='0' AND year(FROM_UNIXTIME(a.modifiedDate))='".$_GET['fromdate']."' AND a.currentStatus='".$type."' ORDER BY a.modifiedDate DESC";
      }        
      else if(($post==='') && ($assignee==='') &&($priority!='')) 
      {
          $shortlist = "select a.*,a.currentAssigneeName,a.id,a.email,a.subject,concat( FROM_UNIXTIME(a.dateReceived)) as receiv,concat( FROM_UNIXTIME(a.modifiedDate)) as modi,a.modifiedBy,a.applicantName,a.priority from trn_emaildetails a 
          where a.deleted='0' AND year(FROM_UNIXTIME(a.modifiedDate))='".$_GET['fromdate']."' AND a.currentStatus='".$type."' AND priority='".$priority."' ORDER BY a.modifiedDate DESC";
      }
      else if(($post==='') && ($assignee!='') &&($priority==='')) 
      {
          $shortlist = "select a.*,a.currentAssigneeName,a.id,a.email,a.subject,concat( FROM_UNIXTIME(a.dateReceived)) as receiv,concat( FROM_UNIXTIME(a.modifiedDate)) as modi,a.modifiedBy,a.applicantName,a.priority from trn_emaildetails a 
                where a.deleted='0' AND year(FROM_UNIXTIME(a.modifiedDate))='".$_GET['fromdate']."' AND a.currentStatus='".$type."' AND currentAssignee='".$assignee."' ORDER BY a.modifiedDate DESC";
      }
      else if(($post!='') && ($assignee==='') &&($priority==='')) 
      {
          $shortlist = "select a.*,a.currentAssigneeName,a.id,a.email,a.subject,concat( FROM_UNIXTIME(a.dateReceived)) as receiv,concat( FROM_UNIXTIME(a.modifiedDate)) as modi,a.modifiedBy,a.applicantName,a.priority from trn_emaildetails a 
                where a.deleted='0' AND year(FROM_UNIXTIME(a.modifiedDate))='".$_GET['fromdate']."' AND a.currentStatus='".$type."' AND post LIKE '%".$post."%' ORDER BY a.modifiedDate DESC";
      }
      else if(($post!='') && ($assignee!='') &&($priority!='')) 
      {
          $shortlist = "select a.*,a.currentAssigneeName,a.id,a.email,a.subject,concat( FROM_UNIXTIME(a.dateReceived)) as receiv,concat( FROM_UNIXTIME(a.modifiedDate)) as modi,a.modifiedBy,a.applicantName,a.priority from trn_emaildetails a 
                where a.deleted='0' AND year(FROM_UNIXTIME(a.modifiedDate))='".$_GET['fromdate']."' AND a.currentStatus='".$type."' AND post LIKE '%".$post."%' AND currentAssignee='".$assignee."'AND priority='".$priority."' ORDER BY a.modifiedDate DESC";
      }
      //  echo json_encode($out->num_rows);
          
          
          $out=mysql_query($shortlist);
             //  $rowShortlist=mysql_fetch_array(mysql_query($shortlist));

            $i=0;
            while($rowShortlist=mysql_fetch_array($out))
            { 
                
                 $date1 = strtotime($rowShortlist['receiv']);
        $date2 = time();
        $subTime = $date1 - $date2;
        $y = ($subTime/(60*60*24*365));
        $d = ($subTime/(60*60*24))%365;
        $h = ($subTime/(60*60))%24;
        $m = ($subTime/60)%60;
        $ela=$d;
        $types = explode("-", $ela);
        //$ela=$types[1].'d '.$types[2].'h '.$types[3].'m';
        $ela=$types[1].' Days ';


                  $arr=  array(
                        "Id"=> $i + 1,
                        "name" => $rowShortlist['applicantName'],
                        "email" =>$rowShortlist['email'],
                        "subject" => $rowShortlist['subject'],
                        "ReceivedDate" => $rowShortlist['receiv'],
                        "ModifiedDate" => $rowShortlist['modi'],
                        "modifyby" => $rowShortlist['modifiedBy'],
                        "priority" => $rowShortlist['priority'],
                        "rwid" => $rowShortlist['id'],
                        "jiraid" => $rowShortlist['jiraTaskID'],
                        "vcode" => $rowShortlist['vcode'],
                        "cus" => $rowShortlist['currentStatus'],
                        "currentass" => $rowShortlist['currentAssignee'],
                        "currentassName" => $rowShortlist['currentAssigneeName'],
                      "elapsed" => $ela
                       );
                    $arr_out[] = $arr;
                    $i++;

            }
           echo json_encode($arr_out);
    }
    else  if($_GET['cat']==='week')
    {        
        $arr_out = array();
        
         if(($post==='') && ($assignee==='') &&($priority===''))
      {
          $shortlist = "select a.*,a.currentAssigneeName,a.id,a.email,a.subject,concat( FROM_UNIXTIME(a.dateReceived)) as receiv,concat( FROM_UNIXTIME(a.modifiedDate)) as modi,a.modifiedBy,a.applicantName,a.priority from trn_emaildetails a 
                where a.deleted='0' AND date(FROM_UNIXTIME(a.modifiedDate))>='".$_GET['fromdate']."' AND date(FROM_UNIXTIME(modifiedDate))<='".$_GET['todate']."' AND a.currentStatus='".$type."' ORDER BY a.modifiedDate DESC";
      }        
      else if(($post==='') && ($assignee==='') &&($priority!='')) 
      {
          $shortlist = "select a.*,a.currentAssigneeName,a.id,a.email,a.subject,concat( FROM_UNIXTIME(a.dateReceived)) as receiv,concat( FROM_UNIXTIME(a.modifiedDate)) as modi,a.modifiedBy,a.applicantName,a.priority from trn_emaildetails a 
          where a.deleted='0' AND date(FROM_UNIXTIME(a.modifiedDate))>='".$_GET['fromdate']."' AND date(FROM_UNIXTIME(modifiedDate))<='".$_GET['todate']."' AND priority='".$priority."' AND a.currentStatus='".$type."' ORDER BY a.modifiedDate DESC";
      }
      else if(($post==='') && ($assignee!='') &&($priority==='')) 
      {
          $shortlist = "select a.*,a.currentAssigneeName,a.id,a.email,a.subject,concat( FROM_UNIXTIME(a.dateReceived)) as receiv,concat( FROM_UNIXTIME(a.modifiedDate)) as modi,a.modifiedBy,a.applicantName,a.priority from trn_emaildetails a 
                where a.deleted='0' AND date(FROM_UNIXTIME(a.modifiedDate))>='".$_GET['fromdate']."' AND date(FROM_UNIXTIME(modifiedDate))<='".$_GET['todate']."' AND currentAssignee='".$assignee."' AND a.currentStatus='".$type."' ORDER BY a.modifiedDate DESC";
      }
      else if(($post!='') && ($assignee==='') &&($priority==='')) 
      {
          $shortlist = "select a.*,a.currentAssigneeName,a.id,a.email,a.subject,concat( FROM_UNIXTIME(a.dateReceived)) as receiv,concat( FROM_UNIXTIME(a.modifiedDate)) as modi,a.modifiedBy,a.applicantName,a.priority from trn_emaildetails a 
                where a.deleted='0' AND date(FROM_UNIXTIME(a.modifiedDate))>='".$_GET['fromdate']."' AND date(FROM_UNIXTIME(modifiedDate))<='".$_GET['todate']."' AND post LIKE '%".$post."%' AND a.currentStatus='".$type."' ORDER BY a.modifiedDate DESC";
      }
      else if(($post!='') && ($assignee!='') &&($priority!='')) 
      {
          $shortlist = "select a.*,a.currentAssigneeName,a.id,a.email,a.subject,concat( FROM_UNIXTIME(a.dateReceived)) as receiv,concat( FROM_UNIXTIME(a.modifiedDate)) as modi,a.modifiedBy,a.applicantName,a.priority from trn_emaildetails a 
                where a.deleted='0' AND date(FROM_UNIXTIME(a.modifiedDate))>='".$_GET['fromdate']."' AND date(FROM_UNIXTIME(modifiedDate))<='".$_GET['todate']."' AND a.currentStatus='".$type."' AND post LIKE '%".$post."%' AND currentAssignee='".$assignee."'AND priority='".$priority."' ORDER BY a.modifiedDate DESC";
      }
        $out=mysql_query($shortlist);
         //  $rowShortlist=mysql_fetch_array(mysql_query($shortlist));
         
        $i=0;
        while($rowShortlist=mysql_fetch_array($out))
        { 
               $date1 = strtotime($rowShortlist['receiv']);
        $date2 = time();
        $subTime = $date1 - $date2;
        $y = ($subTime/(60*60*24*365));
        $d = ($subTime/(60*60*24))%365;
        $h = ($subTime/(60*60))%24;
        $m = ($subTime/60)%60;
        $ela=$d;
        $types = explode("-", $ela);
//        $ela=$types[1].'d '.$types[2].'h '.$types[3].'m';
        $ela=$types[1].' Days ';
            
              $arr=  array(
                    "Id"=> $i + 1,
                    "name" => $rowShortlist['applicantName'],
                    "email" =>$rowShortlist['email'],
                    "subject" => $rowShortlist['subject'],
                    "ReceivedDate" => $rowShortlist['receiv'],
                    "ModifiedDate" => $rowShortlist['modi'],
                    "modifyby" => $rowShortlist['modifiedBy'],
                    "priority" => $rowShortlist['priority'],
                    "rwid" => $rowShortlist['id'],
                    "jiraid" => $rowShortlist['jiraTaskID'],
                    "vcode" => $rowShortlist['vcode'],
                    "cus" => $rowShortlist['currentStatus'],
                  "currentass" => $rowShortlist['currentAssignee'],
                  "currentassName" => $rowShortlist['currentAssigneeName'],
                  "elapsed" => $ela
                   );
                $arr_out[] = $arr;
                $i++;

        }
        echo json_encode($arr_out);
//        echo json_encode($assignee);
        
        
    }
}
 

?>	